﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Common.Lists;
using DNNToolkit.Modules.Glossary.Components;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;

namespace DNNToolkit.Modules.Glossary
{

    public partial class EditCategory : PortalModuleBase, IActionable
    {
        #region Event Handlers

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += (s, e1) =>
            {
                try
                {
                    Localization.LocalizeGridView(ref mCategoryGridView, LocalResourceFile);
                    Localization.LocalizeDetailsView(ref mCategoryDetailsView, LocalResourceFile);
                    if (!IsPostBack)
                        mCategoryDetailsView.Visible = false;
                }
                catch (Exception exc) //Module failed to load
                {
                    Exceptions.ProcessModuleLoadException(this, exc);
                }
            };

            mCategoryGridView.SelectedIndexChanged += (s, e1) =>
            {
                mCategoryDetailsView.Visible = true;
                mCategoryDetailsView.DataBind();
            };

            cmdInsert.Click += (s, e1) =>
            {
                mCategoryDetailsView.Visible = true;
                mCategoryDetailsView.ChangeMode(DetailsViewMode.Insert);
            };

            mCategoryDetailsView.ItemInserted += (s, e1) =>
            {
                mCategoryGridView.DataBind();
            };

            mCategoryDetailsView.ItemUpdated += (s, e1) =>
            {
                mCategoryGridView.DataBind();
            };

            mCategoryDetailsView.ItemDeleted += (s, e1) =>
            {
                mCategoryGridView.DataBind();
            };
            mCategoryDetailsView.ItemCommand += (s, e1) =>
            {
                mCategoryDetailsView.Visible = false;
            };

            mCategoryDetailsView.DataBinding += (s, e1) =>
            {
                RequiredFieldValidator requiredValidatorCategory = mCategoryDetailsView.FindControl("requiredValidatorCategory") as RequiredFieldValidator;
                if (requiredValidatorCategory != null)
                    requiredValidatorCategory.ErrorMessage = Localization.GetString("requiredValidatorCategory.ErrorMessage", this.LocalResourceFile);
            };
        }

        #endregion

        #region IActionable Members


        /// <summary>
        /// Gets the module actions.
        /// </summary>
        /// <value>The module actions.</value>
        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(this.GetNextActionID(), Localization.GetString("Content.Action", this.LocalResourceFile), ModuleActionType.AddContent, "", "view.gif", Globals.NavigateURL(), false, SecurityAccessLevel.Edit, true, false);
                Actions.Add(this.GetNextActionID(), Localization.GetString(ModuleActionType.AddContent, this.LocalResourceFile), ModuleActionType.AddContent, "", "", this.EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion
    }
}